import ServiceTab from '@/editor/js/tab/service-tab'
import TabList from '@/editor/js/tab/tab-list'
import store from '@/store/index'
import  _ from 'lodash'
export default class TabController extends TabList{

    constructor(options){
      super(options);
      _.set(options, 'tabModel', ServiceTab);
      this.tabList_ =new TabList(options);
      var lastWorkedFiles = null;//this.getBrowserStorage().get('workingFileSet'); 存缓存中取
      //this.outputController = _.get(this, 'options.application.outputController');
      this._workingFileSet = [];
      var self = this;
      if (!_.isNil(lastWorkedFiles)) {
        lastWorkedFiles.forEach(function (fileID) {
          self._workingFileSet.push(fileID);
        });
      }
    }

    render = function () {
        //TabList.render(this);
        if (!_.isEmpty(this._workingFileSet)) {
            var self = this;
            this._workingFileSet.forEach(function (fileID) {
                var fileData = self.getBrowserStorage().get(fileID);
                var file = new File(fileData, {});// 去掉了参数{storage = self.getBrowserStorage()}
                var tab = self.newTab(_.set({}, 'tabOptions.file', file));
                tab.updateHeader();
            });
        }
    };

    setActiveTab = function (tab) {
        new TabList().setActiveTab(tab);
    };

    addTab = function (tab) {
        TabList.prototype.addTab.call(this, tab);
        if (tab instanceof ServiceTab && !_.includes(this._workingFileSet, tab.getFile().id)) {
            tab.getFile().save();
            this._workingFileSet.push(tab.getFile().id);
            this.getBrowserStorage().put('workingFileSet', this._workingFileSet);
        }

    };

    removeTab = function (tab, isRemovingAll, isDeletingApp) {
     if(tab!=undefined){
       this.tabList_.removeTab(tab);
     }
    };

    newTab = function (opts) {
        var options = opts || {};
        var tab = new TabList(options).newTab(options);
        return tab;
    };

    goToNextTab = function () {
        if (!_.isEmpty(this._tabs)) {
            var nextTabIndex = 0,
                currentActiveIndex = _.findIndex(this._tabs, this.activeTab);
            if (currentActiveIndex >= 0) {
                if (currentActiveIndex < (this._tabs.length - 1)) {
                    nextTabIndex = currentActiveIndex + 1;
                }
            }
            var nextTab = _.nth(this._tabs, nextTabIndex);
            this.setActiveTab(nextTab);
        }
    };

    goToPreviousTab = function () {
        if (!_.isEmpty(this._tabs)) {
            var currentActiveIndex = _.findIndex(this._tabs, this.activeTab),
                prevTabIndex = 0;
            if (currentActiveIndex == 0) {
                prevTabIndex = this._tabs.length - 1;
            } else {
                prevTabIndex = currentActiveIndex - 1;
            }
            var previousTab = _.nth(this._tabs, prevTabIndex);
            this.setActiveTab(previousTab);
        }
    };

    //通过文件名找到打开的tab页签
    getTabForFile = function (file) {
        return _.find(this._tabs, function (tab) {
            if (tab instanceof ServiceTab) {
                var tabFile = tab.getFile();
                return _.isEqual(tabFile.getPath(), file.getPath()) && _.isEqual(tabFile.getName(), file.getName());
            }
        });
    };

    getTabFromTitle = function (appName) {
        return _.find(this._tabs, function (tab) {
            return tab._title == appName//tab._title == appName + ".siddhi"
        });
    };

    getTabFromId = function (id) {
        return _.find(this._tabs, function (tab) {
            return tab.cid == id
        });
    };

    getBrowserStorage = function () {
        return _.get(this, 'options.application.browserStorage');
    };

    hasFilesInWorkingSet = function () {
        return !_.isEmpty(this._workingFileSet);
    }

}
